package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factorydata.domain.*;
import com.ruoyi.factorydata.mapper.*;
import com.ruoyi.factorydata.service.IMaterialRequisitionDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Optional;

/**
 * 领料单详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-29
 */
@Service
public class MaterialRequisitionDetailServiceImpl extends ServiceImpl<MaterialRequisitionDetailMapper, MaterialRequisitionDetail> implements IMaterialRequisitionDetailService {
    private final MaterialRequisitionDetailMapper materialRequisitionDetailMapper;
    private final ModelInventoryMapper modelInventoryMapper;
    private final MaterialRequisitionCodeDetialMapper materialRequisitionCodeDetialMapper;
    private final MaterialDataMapper materialDataMapper;
    private final WorkOrderInfoMapper workOrderInfoMapper;
    private final MaterialCategoryMapper materialCategoryMapper;
    private final MaterialInventoryMapper materialInventoryMapper;
    private final WarehouseInfoMapper warehouseInfoMapper;
    private final WarehousePositionMapper warehousePositionMapper;
    public MaterialRequisitionDetailServiceImpl(MaterialRequisitionDetailMapper materialRequisitionDetailMapper,
                                                ModelInventoryMapper modelInventoryMapper,
                                                MaterialRequisitionCodeDetialMapper materialRequisitionCodeDetialMapper,
                                                MaterialDataMapper materialDataMapper,
                                                WorkOrderInfoMapper workOrderInfoMapper,
                                                MaterialCategoryMapper materialCategoryMapper,
                                                MaterialInventoryMapper materialInventoryMapper,
                                                WarehouseInfoMapper warehouseInfoMapper,
                                                WarehousePositionMapper warehousePositionMapper) {
        this.materialRequisitionDetailMapper = materialRequisitionDetailMapper;
        this.modelInventoryMapper = modelInventoryMapper;
        this.materialRequisitionCodeDetialMapper = materialRequisitionCodeDetialMapper;
        this.materialDataMapper = materialDataMapper;
        this.workOrderInfoMapper = workOrderInfoMapper;
        this.materialCategoryMapper = materialCategoryMapper;
        this.materialInventoryMapper = materialInventoryMapper;
        this.warehouseInfoMapper = warehouseInfoMapper;
        this.warehousePositionMapper = warehousePositionMapper;
    }

    /**
     * 查询领料单详情
     *
     * @param id 领料单详情主键
     * @return 领料单详情
     */
    @Override
    public MaterialRequisitionDetail selectMaterialRequisitionDetailById(Long id) {
        return materialRequisitionDetailMapper.selectMaterialRequisitionDetailById(id);
    }

    @Override
    public List<MaterialRequisitionDetail> getListByWorkOrderId(Long workOrderId) {
        List<MaterialRequisitionDetail> listByWorkOrderId = materialRequisitionDetailMapper.getListByWorkOrderId(workOrderId);
        for (MaterialRequisitionDetail requisitionDetail : listByWorkOrderId) {
            if(StringUtils.isNotBlank(requisitionDetail.getModelNumber())){
                ModelInventory modelInventory = modelInventoryMapper.selectModelByCode(requisitionDetail.getModelNumber());
                requisitionDetail.setModelInventory(modelInventory);
            }

        }
        return listByWorkOrderId;
    }

    /**
     * 查询领料单详情列表
     *
     * @param materialRequisitionDetail 领料单详情
     * @return 领料单详情
     */
    @Override
    public List<MaterialRequisitionDetail> selectMaterialRequisitionDetailList(MaterialRequisitionDetail materialRequisitionDetail) {
        List<MaterialRequisitionDetail> materialRequisitionDetails = materialRequisitionDetailMapper.selectMaterialRequisitionDetailList(materialRequisitionDetail);
        for (MaterialRequisitionDetail requisitionDetail : materialRequisitionDetails) {
            if(StringUtils.isNotBlank(requisitionDetail.getModelNumber())){
                ModelInventory modelInventory = modelInventoryMapper.selectModelByCode(requisitionDetail.getModelNumber());
                requisitionDetail.setModelInventory(modelInventory);
            }
        }
        return materialRequisitionDetails;
    }

    @Override
    public MaterialRequisitionCodeDetial scanner(String codeSlot) {
        Assert.hasText(codeSlot, "参数不能为空");
        //是否是8+n的数字组合
        Assert.isTrue(codeSlot.matches("\\d{8}\\d+"), "条形码格式错误");
        //截取前8位
        String code = codeSlot.substring(0, 8);
        //后n位
        String slot = codeSlot.substring(8);
        MaterialRequisitionCodeDetial materialRequisitionCodeDetial =materialRequisitionCodeDetialMapper.selectByCodeSlot(code, slot);
        Assert.notNull(materialRequisitionCodeDetial, "未找到对应的领料单");
        MaterialData materialData = materialDataMapper.selectOne(new LambdaQueryWrapper<MaterialData>()
                .eq(MaterialData::getMaterialCode, materialRequisitionCodeDetial.getMaterialCode())
                .last("limit 1")
                .orderByDesc(MaterialData::getId)
        );
        WorkOrderInfo workOrderInfo = workOrderInfoMapper.selectWorkOrderInfoById(materialRequisitionCodeDetial.getWorkOrderId());
        materialRequisitionCodeDetial.setMaterialImages(Optional.ofNullable(materialData).map(MaterialData::getMaterialImages).orElse(null));
        materialRequisitionCodeDetial.setWorkOrderCode(Optional.ofNullable(workOrderInfo).map(WorkOrderInfo::getWorkOrderCode).orElse(null));
        if(materialData!=null&&materialData.getCategoryId()!=null){
            MaterialCategory category = materialCategoryMapper.selectMaterialCategoryById(materialData.getCategoryId());
            if(category!=null){
                materialRequisitionCodeDetial.setMaterialCategoryName(category.getMaterialCategoryName());
            }
        }
        MaterialInventory materialInventory = materialInventoryMapper.selectOne(new LambdaQueryWrapper<MaterialInventory>()
                .eq(MaterialInventory::getBatchNumber, materialRequisitionCodeDetial.getBatchNumber())
                .eq(MaterialInventory::getMaterialCode, materialRequisitionCodeDetial.getMaterialCode())
                .last("limit 1")
                .orderByDesc(MaterialInventory::getId)
        );
        if(materialInventory!=null){
            materialRequisitionCodeDetial.setWarehouseId(materialInventory.getWarehouseId());
            materialRequisitionCodeDetial.setWarehouseCode(materialInventory.getWarehouseCode());
            materialRequisitionCodeDetial.setWarehousePositionId(materialInventory.getWarehousePositionId());
            materialRequisitionCodeDetial.setWarehousePositionCode(materialInventory.getWarehousePositionCode());
            WarehouseInfo warehouseInfo = warehouseInfoMapper.selectWarehouseInfoById(materialInventory.getWarehouseId());
            WarehousePosition warehousePosition = warehousePositionMapper.selectWarehousePositionById(materialInventory.getWarehousePositionId());
            materialRequisitionCodeDetial.setWarehouseName(Optional.ofNullable(warehouseInfo).map(WarehouseInfo::getWarehouseName).orElse(null));
            materialRequisitionCodeDetial.setWarehousePositionName(Optional.ofNullable(warehousePosition).map(WarehousePosition::getPosistionName).orElse(null));
        }
        return materialRequisitionCodeDetial;
    }

    /**
     * 新增领料单详情
     *
     * @param materialRequisitionDetail 领料单详情
     * @return 结果
     */
    @Override
    public int insertMaterialRequisitionDetail(MaterialRequisitionDetail materialRequisitionDetail) {
                materialRequisitionDetail.setCreateTime(DateUtils.getNowDate());
            return materialRequisitionDetailMapper.insertMaterialRequisitionDetail(materialRequisitionDetail);
    }

    /**
     * 修改领料单详情
     *
     * @param materialRequisitionDetail 领料单详情
     * @return 结果
     */
    @Override
    public int updateMaterialRequisitionDetail(MaterialRequisitionDetail materialRequisitionDetail) {
                materialRequisitionDetail.setUpdateTime(DateUtils.getNowDate());
        return materialRequisitionDetailMapper.updateMaterialRequisitionDetail(materialRequisitionDetail);
    }

    /**
     * 批量删除领料单详情
     *
     * @param ids 需要删除的领料单详情主键
     * @return 结果
     */
    @Override
    public int deleteMaterialRequisitionDetailByIds(Long[] ids) {
        return materialRequisitionDetailMapper.deleteMaterialRequisitionDetailByIds(ids);
    }

    /**
     * 删除领料单详情信息
     *
     * @param id 领料单详情主键
     * @return 结果
     */
    @Override
    public int deleteMaterialRequisitionDetailById(Long id) {
        return materialRequisitionDetailMapper.deleteMaterialRequisitionDetailById(id);
    }
}